package com.yjzx.util.common.util.BeanUtils;

import com.yjzx.util.common.util.StringUtils;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;

/**
 * @author yjzx
 * @date 2024/8/14
 * @description LambdaFieldSetter
 */
public interface LambdaFieldSetter extends LambdaField {
    @Override
    default Method getColumnSetterMethod() {
        return getMethodFromLambda();
    }

    @Override
    default Method getColumnGetterMethod() {
        Class<?> aClass = this.getClassFromLambda();

        Field field = this.getLambdaColumn();
        if (field != null) {
            String columnName = field.getName();
            String methodNameGet = PREFIX_GETTER_GETTER + columnName;
            String methodNameIs = PREFIX_GETTER_IS + columnName;
            try {
                if (Boolean.class.equals(field.getType()) || Boolean.TYPE.equals(field.getType())) {
                    return aClass.getMethod(methodNameIs);
                } else {
                    return aClass.getMethod(methodNameGet);
                }
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }

        return null;
    }

    /**
     * 获取setter方法的属性对象
     *
     * @return
     */
    @Override
    default Field getLambdaColumn() {
        try {
            SerializedLambda serializedLambda = getSerializedLambda();
            // 获取方法名称
            String setter = serializedLambda.getImplMethodName();
            Class<?> declaringClass = getClassByClassName(serializedLambda.getImplClass());
            String name = null;
            if (setter != null) {
                if (setter.startsWith(PREFIX_SETTER)) {
                    name = setter.replace(PREFIX_SETTER, "");

                }
            }
            getField(declaringClass, name);
        } catch (ClassNotFoundException e) {
        }
        return null;
    }

}
